package com.example.love4ever.login;

import com.example.love4ever.dao.UserMapper;
import com.example.love4ever.model.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 登录验证
 *
 * @Author xz
 * @Date 2020/8/19 11:04
 */
@RestController
@RequestMapping("/login")
public class LoginHandler {

    @Autowired
    UserMapper userMapper;

    @ResponseBody
    @RequestMapping("/check")
    public User getPhotograph(@RequestBody User param) {
        User user = new User();
        user.setShortName(param.getName());
        user.setPassword(param.getPassword());
        List<User> login = userMapper.login(user);
        if (login.size() > 0) {
            BeanUtils.copyProperties(login.get(0), user);
            user.setCode("succ");
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        User logUser = new User();
        logUser.setName(param.getName());
        logUser.setMsg("登录");
        logUser.setTime(df.format(new Date()));
        userMapper.insertLog(logUser);
        return user;
    }

}
